package com.suineg.pms.service.base;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.suineg.pms.entity.base.Room;
import com.suineg.pms.repository.base.RoomDao;
import com.suineg.pms.util.StringUtils;

/**
 * 房屋信息管理 
 *@author Suine
 */
@Service
@Transactional
public class RoomService {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private RoomDao dao;
	
	public List<Room> findAll(){
		return Lists.newArrayList(dao.findAll());
	}
	
	public Room get(Long id){
		return dao.findOne(id);
	}
	
	public void save(Room room){
		dao.save(room);
	}
	
	public void delete(Long id){
		dao.delete(id);
	}
	
	public Room findByHouseCode(String houseCode){
		return dao.findByHouseNo(houseCode);
	}
	
	/**
	 * 根据楼栋、单元、室号找到ROOM实体
	 * @param id
	 * @param unit
	 * @param roomNo
	 * @return
	 */
	public List<Room> findByBuildingAndUnitAndRoomNo(Long id,String unit,String roomNo){
		return dao.findByBuildingAndUnitAndRoomNo(id, unit, roomNo);
	}
	
	/**
	 * 根据房屋编号 查询室信息
	 * @param houseNos 以这种形式 编号1,编号2,编号3
	 * @return
	 */
	public List<Room> findRoomByHouseNo(String houseNos){
		return dao.findRoomByHouseNos(houseNos);
	}
	
	/**
	 * 根据楼栋获取 新的单元号
	 * <p>
	 * 	最大的单元号 + 1
	 * </p>
	 * @param buildingId
	 * @return 
	 */
	public String findMaxUniNoByBuidlingId(Long buildingId){
		return dao.findMaxUnitNoByBuidlingId(buildingId);
	}
	
	public void saveAll(List<Room> rooms){
		dao.save(rooms);
	}
	
	/**
	 * 获取一个楼栋中，一层的户数 最多一户数
	 * @param id
	 * @return
	 */
	public Integer findRooMaxByBuidingId(Long id,String unit){
		String ss = dao.findRooMaxByBuidingId(id,unit);
//		List<Object[]> list = dao.findTest();
//		for(Object[] l : list){
//			for(Object o : l){
//				logger.info(o.toString());
//			}
//		}
		
//		List<Map> list = dao.findTest();
//		for(Map m : list){
//			logger.info(m.keySet().toString());
//		}
//		logger.info("这儿" + list.toString());
		if(StringUtils.isEmpty(ss)){
			return 0;
		}
		return Integer.valueOf(ss);
	}

}

